//
//  YDOutOptionsViewController.m
//  PeiZiWangBei
//
//  Created by 巨彦新 on 2018/3/2.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDOutOptionsViewController.h"
#import "YDOutOptionsCell.h"
#import "YDOutOptionsM.h"
#import "YDBuyOptionsViewController.h"


@interface YDOutOptionsViewController ()<UITableViewDelegate,UITableViewDataSource,SYNoDataViewDelegate>

@property (nonatomic, strong)UITableView *tableView;


/**
 数据源
 */
@property (nonatomic, strong)NSMutableArray *outOptionsList;
/**
 当前页码
 */
@property (nonatomic , assign) NSInteger currentPage;


/**
 无数据
 */
@property (nonatomic , weak) SYNoDataView *dataView;

//引导登录页
@property (nonatomic, weak) SYGuideLoginView *guideLoginView;

//顶部安全区域
@property (nonatomic , assign) CGFloat safeTopH;
//底部安全区域
@property (nonatomic , assign) CGFloat safeBottomH;

@property (nonatomic, strong)SYNoNetView *noNetView;

@end

@implementation YDOutOptionsViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:mainGray];
    self.safeTopH = isPhoneX ? 84 : 64;
    self.safeBottomH = self.isHiddenTabbar ? 0 : (isPhoneX ? 83 : 49);
    [self tableView];
//    [self loadData];
    [self setUpDataManger];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([SYUserInformation sharedInstance].isLogin)
    {
        [self loadData];
        [self.guideLoginView removeFromSuperview];
        
    }else{
        [self.outOptionsList removeAllObjects];
        [self.tableView reloadData];
        [self guideLoginView];
    }
}
- (void)removeNodataView
{
    if (_dataView != nil) {
        [_dataView removeFromSuperview];
        _dataView = nil;
    }
}
- (void)removeNoNetView
{
    if (_noNetView != nil) {
        [_noNetView removeFromSuperview];
        _noNetView = nil;
    }
}


-(NSMutableArray *)outOptionsList
{
    if (!_outOptionsList) {
        _outOptionsList = [NSMutableArray array];

    }
    return _outOptionsList;
}
-(void)setUpDataManger
{
    MJGIFHeader *header = [MJGIFHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    //隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES ;
    //隐藏状态
    header.stateLabel.hidden = YES ;
    self.tableView.mj_header = header;
    
    MJGIFFooter *footer = [MJGIFFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    footer.stateLabel.hidden = YES;
    self.tableView.mj_footer = footer;
    self.tableView.mj_footer.hidden = YES;
    
    
}
- (void)loadData
{

    self.currentPage = 0;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@(self.currentPage) forKey:@"page"];
    [dict setObject:@(10) forKey:@"size"];
    
    [BaseNetRequest sessionRequestWithparameters:dict method:GETMethod requstType:OptionOutPositionReword WithSuccessBlock:^(NSDictionary *resultDict) {
        
        [self.tableView.mj_header endRefreshing];
        if ([resultDict[@"code"] integerValue] == 200) {
            
            [self.outOptionsList removeAllObjects];
            self.outOptionsList = [YDOutOptionsM mj_objectArrayWithKeyValuesArray:resultDict[@"result"][@"content"]];
            

            if (self.outOptionsList.count < 10) {
                self.tableView.mj_footer.hidden = NO;
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                self.tableView.mj_footer.hidden = NO;
                 [self.tableView.mj_footer endRefreshing];
            }

            [self.tableView reloadData];


            if (self.outOptionsList.count) {
                [self removeNodataView];
                [self removeNoNetView];

            }else
            {
                [self removeNoNetView];
                [self dataView];

            }
        }
        else
        {
            [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:resultDict[@"message"] isExistNav:YES];
            if (!self.outOptionsList.count) {
                [self removeNodataView];
                [self noNetView];
            }
        }
    } failBlock:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"信息加载出错，请重试~" isExistNav:YES];
        if (!self.outOptionsList.count) {
            [self removeNodataView];
            [self noNetView];
        }
    }];
}

-(void)loadMoreData
{
   
    self.currentPage++;
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@(self.currentPage) forKey:@"page"];
    [dict setObject:@(10) forKey:@"size"];

    
    [BaseNetRequest sessionRequestWithparameters:dict method:GETMethod requstType:OptionOutPositionReword WithSuccessBlock:^(NSDictionary *resultDict) {
        
        [self.tableView.mj_footer endRefreshing];
        if ([resultDict[@"code"] integerValue] == 200) {

            NSArray *newsArr = [YDOutOptionsM mj_objectArrayWithKeyValuesArray:resultDict[@"result"][@"content"]];
            
            [self.outOptionsList addObjectsFromArray:newsArr];

            if (newsArr.count < 10) {
                self.tableView.mj_footer.hidden = NO;
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                self.tableView.mj_footer.hidden = NO;
                [self.tableView.mj_footer endRefreshing];
            }
            [self.tableView reloadData];


        }
        else
        {
            [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:resultDict[@"message"] isExistNav:YES];

        }
    } failBlock:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"信息加载出错，请重试~" isExistNav:YES];
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.outOptionsList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    YDOutOptionsCell *cell = [YDOutOptionsCell cellWithTbaleview:tableView];
    cell.model = [self.outOptionsList objectAtIndex:indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    YDOutOptionsM *model = [self.outOptionsList objectAtIndex:indexPath.row];
    if (!model.isSelected) {
        return isPhoneX ? rateX812(154) : rate750(154);
    }else
    {
        return isPhoneX ? rateX812(384) : rate750(384);
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDOutOptionsM *model = [self.outOptionsList objectAtIndex:indexPath.row];
    model.isSelected = !model.isSelected;
    
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationFade];
    
}

/*******tableView********/
-(UITableView *)tableView
{
    if (_tableView == nil) {

        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, LKScreenW, LKScreenH - self.safeTopH - self.safeBottomH) style:UITableViewStylePlain];
        
        [self.view addSubview:tableView];
        tableView.backgroundColor = [UIColor colorWithHexString:mainGray];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.showsVerticalScrollIndicator = NO;
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.tableFooterView = [UIView new];
        if (isIos11) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView = tableView;
    }
    return _tableView;
}

#pragma SYNoDataViewDelegate
-(void)noDataView:(SYNoDataView *)noDataView didClickSaleButton:(SYNoHeightedButton *)button
{
    YDBuyOptionsViewController *vc = [[YDBuyOptionsViewController alloc] init];
    vc.stockCode = @"000001";
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    DDLog(@"期权持仓界面期权交易")
}
/*******dataView********/
-(SYNoDataView *)dataView
{
    if (_dataView == nil) {
        CGFloat dataW = rate375(240);
        CGFloat dataX = (LKScreenW - dataW) * 0.5;
        CGFloat dataH = isPhoneX ? rateX812(276) : rate750(276);
        CGFloat dataY = isPhoneX ? rateX812(100) : rate750(100);
        CGRect frame = CGRectMake(dataX, dataY, dataW, dataH);
        //wuneirong   wuxinhao
        SYNoDataView *dataView = [SYNoDataView positionNoDataViewInitWithFrame:frame ImageString:@"wuneirong" buttonTitle:@"策略申购" IsCanSeeBuyButton:YES];
        dataView.delegate = self;
        dataView.isCanSeeBuyButton = YES;
        [self.tableView addSubview:dataView];
        _dataView = dataView;
    }
    return _dataView;
}

-(SYGuideLoginView *)guideLoginView
{
    if (_guideLoginView == nil) {
        //没有登录贴引导登录展位图
        SYGuideLoginView *guideLoginView = [[SYGuideLoginView alloc] init];
        [self.view addSubview:guideLoginView];
        [guideLoginView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.equalTo(isPhoneX ? @(rateX812(40)) : @(rate750(40)));
        }];
        LK(weself)
        [guideLoginView setClickBlock:^(BOOL isClick) {
            YDLoginController *loginVC = [[YDLoginController alloc] init];
            loginVC.loginStyle = Login_Present;
            YDBaseNavigationController *baseNav = [[YDBaseNavigationController alloc] initWithRootViewController:loginVC];
            [weself presentViewController:baseNav animated:YES completion:nil];
        }];
        _guideLoginView = guideLoginView;
    }
    return _guideLoginView;
}

-(SYNoNetView *)noNetView
{
    if (_noNetView == nil) {
        SYNoNetView *noNetView = [[SYNoNetView alloc] init];
        [self.tableView addSubview:noNetView];
        
        [noNetView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.equalTo(isPhoneX ? @(rateX812(40)) : @(rate750(40)));
        }];
        LK(weself)
        [noNetView setClickBlock:^(BOOL isClick) {
            [weself.tableView.mj_header beginRefreshing];
            
        }];
        _noNetView = noNetView;
    }
    return _noNetView;
}
@end
